package com.hpsk.shopservice.model.config.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.nio.file.LinkOption;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @author huangwang2832511019
 */
@Service
@Slf4j
public class TestAsyncService {
    @Async("mybatisExecutor")
    public void asyncMethod(int sleepTime) {
        sleep(sleepTime);
        log.info("异步方法内部线程名称：{}", Thread.currentThread().getName());
    }

    @Async("mybatisExecutor")
    public Future<String> asyncFutureMethod(String param) {
        sleep(10);
        log.info("异步方法内部线程名称：{}", Thread.currentThread().getName());
        return new AsyncResult<>(param);
    }

    public void syncMethod() {
        sleep(10);
    }

    private void sleep(int sleepTime) {
        try {
            TimeUnit.SECONDS.sleep(sleepTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
